package cn.yunyichina.provider.pay.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 微信 Excel 文件解析工具类
 * Created by JoeZ on 2016/10/11.
 */
public class WechatCSVFileUtil {

    private InputStreamReader fr = null;
    private BufferedReader br = null;

    public WechatCSVFileUtil(String f) throws IOException {
        fr = new InputStreamReader(new FileInputStream(f), "UTF-8");
    }

    /**
     * 微信账单解析
     */
    public List<List<String>> weichatReadCSVFile() throws IOException {
        br = new BufferedReader(fr);
        String rec = null;// 一行
        String str;// 一个单元格
        List<List<String>> listFile = new ArrayList<List<String>>();
        try {
            int count = 0 ;
            // 读取一行
            while ((rec = br.readLine()) != null) {
                List<String> cells = new ArrayList<String>();// 每行记录一个list
                rec = rec.replaceAll("`", "");
                String[] split = rec.split("\\,");

                for (int i = 0; i < split.length; i++) {
                    // 读取每个单元格
                    cells.add(new String(split[i].trim().toString().getBytes("UTF-8")));
                }

                //去除非账单条目逻辑
                if(count > 0){
                    if(rec.contains("总交易单数")){
                        break;
                    }
                    listFile.add(cells);
                }

                count ++ ;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                fr.close();
            }
            if (br != null) {
                br.close();
            }
        }
        return listFile;
    }

}
